package com.dcbeida.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.DownloadActivity;
import com.dcbeida.activity.GzhActivity;
import com.dcbeida.activity.HelpActivity;
import com.dcbeida.activity.HomeActivity;
import com.dcbeida.activity.ProfileActivity;
import com.dcbeida.activity.changepassword.ChangePasswordActivity;
import com.dcbeida.activity.clerkmanager.AccountListActivity;
import com.dcbeida.activity.clerkmanager.ClerkStatueActivity;
import com.dcbeida.activity.login.LoginWithCodeActivity;
import com.dcbeida.listern.DialogListener;
import com.dcbeida.mine.MyClientActivity;
import com.dcbeida.utils.DialogUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 我的
 * Created by swt on 2016/11/17.
 */

public class MineFragment extends BaseFragment {


    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    @Bind(R.id.ll_wdzl)
    LinearLayout llWdzl;
    @Bind(R.id.ll_xgmm)
    LinearLayout llXgmm;
    @Bind(R.id.ll_wdkh)
    LinearLayout llWdkh;
    @Bind(R.id.tv_tcdl)
    TextView tvTcdl;
    @Bind(R.id.ll_lxcj)
    LinearLayout llLxcj;
    @Bind(R.id.tv_version)
    TextView tv_version;
    private Object data;
    @Bind(R.id.ll_manage)
    LinearLayout ll_manage;

    @Bind(R.id.ll_gzh)
    LinearLayout ll_gzh;
    @Bind(R.id.ll_bz)
    LinearLayout ll_bz;
    @Bind(R.id.ll_download)
    LinearLayout ll_download;
    @Bind(R.id.lin_change_password)
    LinearLayout lin_change_password;



    @Override
    public View initViews() {
        View view = View.inflate(mActivity, R.layout.mine_fragment, null);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void initData() {
        tvRight.setVisibility(View.GONE);
        tvTitle.setText(R.string.wd);
        if (MyApplication.user.getType() == 0) {
            ll_manage.setVisibility(View.VISIBLE);
        } else {
            ll_manage.setVisibility(View.GONE);
        }
        try {
            PackageInfo packageInfo = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0);
            String version = packageInfo.versionName;
            tv_version.setText(getString(R.string.ver, version));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {   // 不在最前端显示 相当于调用了onPause();
        } else {  // 在最前端显示 相当于调用了onResume();
            //刷新数据

        }
    }


    @OnClick({R.id.ll_wdzl, R.id.ll_xgmm, R.id.ll_wdkh, R.id.tv_tcdl, R.id.ll_lxcj, R.id.ll_manage,R.id.lin_change_password,
    R.id.ll_gzh,R.id.ll_bz,R.id.ll_download})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_wdzl:
                if (MyApplication.user.getType() == 0) {
                    ((HomeActivity) getActivity()).showActivity(ProfileActivity.class, null);
                } else {
                    ((HomeActivity) getActivity()).showActivity(ClerkStatueActivity.class, null);
                }
                break;
            case R.id.ll_xgmm:
                ((HomeActivity) getActivity()).showActivity(ChangePasswordActivity.class, null);
                break;
            case R.id.ll_wdkh:
                ((HomeActivity) getActivity()).showActivity(MyClientActivity.class, null);
                break;
            case R.id.lin_change_password:
                ((HomeActivity) getActivity()).showActivity(ChangePasswordActivity.class, null);
                break;
            case R.id.tv_tcdl:
                DialogUtils.showConfirmAndCancel(getActivity(), "确定要退出登录？", new DialogListener() {
                    @Override
                    public void onCancl(Dialog dialog) {
                        dialog.dismiss();
                    }

                    @Override
                    public void onConfirm(Dialog dialog) {
                        dialog.dismiss();
                        ((HomeActivity) getActivity()).showActivity(LoginWithCodeActivity.class, null);
                    }
                }, R.string.cancel, R.string.confim);


                break;
            case R.id.ll_lxcj:
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "17300935806"));
                startActivity(intent);
                break;
            case R.id.ll_manage:
                ((HomeActivity) getActivity()).showActivity(AccountListActivity.class, null);
                break;
            case R.id.ll_bz:
                ((HomeActivity) getActivity()).showActivity(HelpActivity.class, null);
                break;
            case R.id.ll_gzh:
                ((HomeActivity) getActivity()).showActivity(GzhActivity.class, null);
                break;
            case R.id.ll_download:
                ((HomeActivity) getActivity()).showActivity(DownloadActivity.class, null);
                break;
        }
    }
}
